﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SASystem.SAServiceRef;

namespace SASystem
{
    public partial class FamilyDetailFrm : Form
    {
        private FamilyInfo m_familyInfo;
        private string m_tempFolder;
        public FamilyDetailFrm(FamilyInfo familyInfo)
        {
            InitializeComponent();
            m_familyInfo = familyInfo;
        }

        private void FamilyDetailFrm_Load(object sender, EventArgs e)
        {
            //get data.
            FamilyAttrInfo familyAttr = SABLL.GetFamilyAttrInfo(m_familyInfo.ID);

            if (familyAttr == null)
            {
                SABLL.ShowErrMsg(SABLL.GetLastErrMsg());
                return;
            }

            string templateFilePath, modelPicPath, drawPicPath;
            templateFilePath = System.IO.Path.Combine(GetTempFolder(), familyAttr.TemplateFileName);
            modelPicPath = System.IO.Path.Combine(GetTempFolder(), familyAttr.ModelPicName);
            drawPicPath = System.IO.Path.Combine(GetTempFolder(), familyAttr.DrawPicName);

            templateFilePath = SABLL.SaveBinaryByPath(templateFilePath, familyAttr.TemplateFileBinary);
            modelPicPath = SABLL.SaveBinaryByPath(modelPicPath, familyAttr.ModelPicBinary);
            drawPicPath = SABLL.SaveBinaryByPath(drawPicPath, familyAttr.DrawPicBinary);

            this.pictureBox_modelPic.ImageLocation = modelPicPath;
            this.pictureBox_drawPic.ImageLocation = drawPicPath;
            this.textBox_modelName.Text = m_familyInfo.Name;
            this.textBox_category.Text = SABLL.GetCategoryInfoByCateId(m_familyInfo.CategoryID).Name;

            VendorInfo vendor = SABLL.GetVendor(m_familyInfo.VendorID);
            if (vendor != null) this.textBox_vendor.Text = vendor.Name;

            StandardInfo standard = SABLL.GetStandard(m_familyInfo.StandardID);
            if (standard != null) this.textBox_standard.Text = standard.Name;

            this.richTextBox_description.Text = familyAttr.Description;

            List<ParameterInfo> keyParameters = SABLL.GetFamilyKeyParameters(m_familyInfo.ID);
            if (keyParameters == null)
            {
                SABLL.ShowErrMsg(SABLL.GetLastErrMsg());
                return;
            }
            else
            {
                SABLL.InitKeyParametersGridView(this.dataGridView_driverParamsList, keyParameters);
            }
        }

        private string GetTempFolder()
        {
            if (String.IsNullOrEmpty(m_tempFolder))
            {
                string retPath;
                retPath = System.IO.Path.GetTempPath();
                retPath = System.IO.Path.Combine(retPath, m_familyInfo.ID.ToString());
                m_tempFolder = retPath;
            }

            return m_tempFolder;
        }
    }
}
